package interfaz_usuario;
import java.awt.Color;

import java.awt.Graphics2D;



import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EventObject;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.plaf.basic.BasicTreeUI.TreeCancelEditingAction;

import objects.WorldObject;
import universe.Universe;

import eventos.EventInfObjectBar;
import eventos.ObjectBarClickedListener;
import eventos.ObjectBarEvent;
import extra.Position;

import factoryNCastle.ObjectFactory;
import constants.*;




public class PaintPanel extends JPanel implements Runnable , ObjectBarClickedListener{
	
	
	private int x=300,y=300;
	private int height=600;
	private int width=600;
	
	private ArrayList<WorldObject> imagesList;
	private WorldObject ima;
	private Universe world;
	
	//graphic to paint pixels  not free  
	private Graphics graph;
	private BufferedImage image;
	private int freeColor;
	private int dy=0;
	private int dx=0;
	
	private PopUpObjectMenu popUp;
	
	public PaintPanel(){
		
		setVisible(true);
		
		ima = ObjectFactory.createWorldObject(Constants.OBJECT_REFERENCE_BOMBERMAN);
		world =  new Universe(1, 1);
		
		//createCollitionBuffer();
		
		addMouseListener(new mouseListener());
		addMouseMotionListener(new mouseMotionListener());
		
		addKeyListener(new keyListener());
		
		
	}
	
	/*private void createCollitionBuffer(){
		image = new BufferedImage(world.getScreenWidth(), world.getScreenHeight(), BufferedImage.TYPE_INT_RGB);
		graph= image.createGraphics();
		graph.setColor(new Color(237,2,53));
		graph.fillRect(0, 0, world.getScreenWidth(), world.getScreenHeight());
		freeColor= graph.getColor().getRGB();
		world.generateCollitions();
	}*/
	
	@Override
	public void paint(Graphics g) {
		
		
		g.clearRect(0, 0, 1000, 1000);
		
		Graphics2D g2d = (Graphics2D) g;
		AffineTransform prev = g2d.getTransform();
		AffineTransform at =prev;
		
		if((x+width/2)>=world.getScreenWidth()){
			
			if(width<world.getWidth()){
				dx=((width)-world.getScreenWidth());
			}
		}
		else{
			if(x>=(width/2) ){					
				dx=(width/2)-(x);
			}
		}

		//PARA LAS Y
		
		if((y+height/2)>=world.getScreenHeight()){
			
			if(height<world.getScreenHeight()){
				dy=((height)-world.getScreenHeight());
			}
		}
		else{
			if(y>=(height/2) ){
				dy=(height/2)-(y);
			}
		}
		
		at=AffineTransform.getTranslateInstance(dx,dy);
		g2d.setTransform(at);
		
		
			world.paint(g);
			Collections.sort(imagesList);
			for(WorldObject obj : imagesList){
				obj.paint(g);
			}
			ima.paint(g);			
		
		g2d.setTransform(prev);

	}
	
	private class mouseMotionListener implements MouseMotionListener{

		@Override
		public void mouseDragged(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseMoved(MouseEvent ev) {
			// TODO Auto-generated method stub
			ima.setPosition(new Position((ev.getX()-dx), (ev.getY()-dy)));
			
			//System.out.println("MEMoci "+ev.getX()+"  otro "+ev.getY());
		}
		
	}
	
	private class mouseListener implements MouseListener{

		@Override
		public void mouseClicked(MouseEvent ev) {
			
			if(ev.getButton() == MouseEvent.BUTTON1){
				world.addObjectCopy(ima);
				world.generateCollitions();
				/*ima.paintCollitionShape(graph);
				world.CalculateIntersection(image,freeColor);*/
				
			grabFocus();
			}
			if(ev.getButton() == MouseEvent.BUTTON3){
				System.out.println("Click izquierdo");
				WorldObject obj = world.clickObject(ev.getX()-dx,ev.getY()-dy);
				if(obj!=null){
					//System.out.println("Soy X: "+obj.getX() +" Y "+obj.getY());
					System.out.println("Click izquierdo1");
					popUp = new PopUpObjectMenu(obj);
					popUp.setSize(200, 200);
					showPopUp(ev.getX(),ev.getY());
				}
				
			}
			
		}

		@Override
		public void mouseEntered(MouseEvent ev) {
			
			
			//Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(miraImagen , new Point(10,10), "Mira");
			//setCursor(cursor);
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	}

	private class keyListener implements KeyListener{

		@Override
		public void keyPressed(KeyEvent e) {
			System.out.println("KeyPressed");
			if(e.getKeyCode() == KeyEvent.VK_LEFT){
				
				x=x-10;
			}
			if(e.getKeyCode() == KeyEvent.VK_RIGHT){
				
				x=x+10;
			}
			if(e.getKeyCode() == KeyEvent.VK_UP){
				
				y=y-10;
			}
			
			if(e.getKeyCode() == KeyEvent.VK_DOWN){
				
				y=y+10;
			}
			
			
		}

		@Override
		public void keyReleased(KeyEvent arg0) {
			// TODO Auto-generated method stub
			System.out.println("KeyReleased");
			
		}

		@Override
		public void keyTyped(KeyEvent arg0) {
			// TODO Auto-generated method stub
			System.out.println("Keytyped");
		}
		
	}
	
	@Override
	public void run() {
		
			//era 20
		while(true){
			try {
				Thread.sleep(100);
				imagesList=world.getListObjects();
				
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			repaint();
			
		}
		
	}

	@Override
	public void changeObject(ObjectBarEvent event) {
		System.out.println("2"+event);
		
		EventInfObjectBar ev = (EventInfObjectBar) event.getSource();
		System.out.println("3"+ev.getReference());
 		ima=ObjectFactory.createWorldObject(ev.getReference());
 				
		
	}
	/*public void setWorldSize (int width, int height){
		world.setHeight(height);
		world.setWidth(width);
		
	}*/

	public void newWorld(int width, int height) {
		world = new Universe(width, height);
		
		repaint();
		
	}

	public String save() {
		String save = new String();
		
		save = world.saveFile(save);
		
		return save;
	}

	public void load(String name) {
		world.loadFile(name);
		
	}

	
	
	private void showPopUp(int x, int y) {
		System.out.println("Muestro popUp");
		popUp.show(this,x,y);
		
	}

}

